package com.fit.pages;

import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.beans.CarBean;
import com.fit.enums.AccountRole;
import com.fit.interfaces.CarI;
import com.fit.interfaces.ServiceI;
import com.fit.interfaces.User;
import com.fit.service.CarsBean;
import com.fit.service.CarsI;
import com.fit.service.ServicesI;

public class EditCarService {

	@SessionState
	private User user;
	
	private boolean userExists;

	public User getUser() {
		return user;
	}
	
	@Persist
	@Property
	private ServiceI currentService;
	
	@Inject
	private ServicesI services;
	
	@Inject
	private CarsI cars;
	
	Object onActivate(){
		if(!userExists) return Login.class;
		if(user.getRole() == AccountRole.USER) return Index.class;
		return null;
	}
	
	Object onActivate(long id){
		if(!userExists) return Login.class;
		if(user.getRole() == AccountRole.USER) return Index.class;
		currentService = services.retriveSingleService(id);
		return null;
	}
	
	Object onSuccess(){
		currentService.getCarService().setServiceDone(false);
		cars.editSingleCar(currentService.getCarService());
		services.editSingleService(currentService);
		
		return BrowseService.class;
	}
}
